//
//  BaseTabbarController.m
//  SSFS
//
//  Created by Hero on 16/9/22.
//  Copyright © 2016年 Hero. All rights reserved.
//

#import "BaseTabbarController.h"
#import "BaseNavController.h"
#import "HomeViewController.h"
#import "TeachingViewController.h"
#import "WorkViewController.h"
#import "MineViewController.h"
#import "MineViewControllert.h"
#import "BaseTabBarModel.h"
#import "LiveViewController.h"
#import "StudyViewController.h"
@interface BaseTabbarController ()<UITabBarControllerDelegate>
@property (nonatomic ,strong) NSMutableArray *dataSurce;
@property (nonatomic ,strong) NSMutableArray *titleArr;
@end

@implementation BaseTabbarController
- (void)viewDidLoad {
    [super viewDidLoad];
//    [self setUpAllChildViewController];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:UIColorFromRGB(0x999999),NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:UIColorFromRGB(0x1A7BE5),NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    self.delegate = self;
    [self loadData];
    [self setUpAllChildViewController];
}
- (void)loadData{
    
    NSDictionary *params = @{@"roleid":[us objectForKey:kRoleid]};
    [NetWorkManager requestWithType:1 withUrlString:@"/appmenu/appgetMenu.do" withParaments:params withSuccessBlock:^(id object) {
        NSInteger count = 0;
        for (NSDictionary *dic in object[@"menuList"]) {
            count++;
            BaseTabBarModel *model = [BaseTabBarModel modelWithDict:dic];
            if (count ==2) {
                [us setObject:model.MENUID forKey:kStudentsMenuId2];
            }if (count ==3) {
               [us setObject:model.MENUID forKey:kStudentsMenuId3];
            }if(count ==4){
            [us setObject:model.MENUID forKey:kStudentsMenuId4];
            }
            [self.dataSurce addObject:model];
        }
    } withFailureBlock:^(NSError *error) {
    } progress:^(float progress) {
        
    }];
   

    
}
- (void)setUpAllChildViewController{
    HomeViewController *homeVC = [[HomeViewController alloc]init];
    MineViewController *mineVC = [[MineViewController alloc]init];
    MineViewControllert *minetVC = [[MineViewControllert alloc]init];
    //教师
    TeachingViewController *teachVC = [[TeachingViewController alloc]init];
    WorkViewController *workVC = [[WorkViewController alloc]init];
   //学生
    LiveViewController *liveVC = [[LiveViewController alloc]init];
    StudyViewController *studyVC = [[StudyViewController  alloc]init];
    if ([[us objectForKey:kRoleid] isEqualToString:@"2"]) {//教师
            [self setUpOneChildViewController:homeVC image:[UIImage imageNamed:@"首页2-1"] selectedImage:[UIImage imageNamed:@"首页-选中"] title:@"首页" NavTitle:@"智慧校园"];
            [self setUpOneChildViewController:teachVC image:[UIImage imageNamed:@"教学-1"] selectedImage:[UIImage imageNamed:@"教学-选中"] title:@"教学" NavTitle:@"智慧教学"];
            [self setUpOneChildViewController:workVC image:[UIImage imageNamed:@"办公-1"] selectedImage:[UIImage imageNamed:@"办公-选中"] title:@"办公" NavTitle:@"智慧办公"];
        
                [self setUpOneChildViewController:mineVC image:[UIImage imageNamed:@"我的-1"] selectedImage:[UIImage imageNamed:@"我的-选中"] title:@"我的" NavTitle:@"我的"];
            }else{
                [self setUpOneChildViewController:homeVC image:[UIImage imageNamed:@"首页2-1"] selectedImage:[UIImage imageNamed:@"首页-选中"] title:@"首页" NavTitle:@"智慧校园"];
                [self setUpOneChildViewController:studyVC image:[UIImage imageNamed:@"教学-1"] selectedImage:[UIImage imageNamed:@"教学-选中"] title:@"学习" NavTitle:@"智慧学习"];
                [self setUpOneChildViewController:liveVC image:[UIImage imageNamed:@"办公-1"] selectedImage:[UIImage imageNamed:@"办公-选中"] title:@"生活" NavTitle:@"智慧生活"];
            
                 [self setUpOneChildViewController:minetVC image:[UIImage imageNamed:@"我的-1"] selectedImage:[UIImage imageNamed:@"我的-选中"] title:@"我的" NavTitle:@"我的"];
            
            
            }
   

}

- (void)setUpOneChildViewController:(UIViewController *)viewController image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title NavTitle:(NSString *)titleNav{
    viewController.tabBarItem.title = title;
    viewController.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    BaseNavController *nav = [[BaseNavController alloc] initWithRootViewController:viewController];
    viewController.navigationItem.title = titleNav;
    [self addChildViewController:nav];
}
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
}
#pragma mark - 懒加载 -
- (NSMutableArray *)dataSurce{
    if (!_dataSurce) {
        _dataSurce = [NSMutableArray array];
    }
    return _dataSurce;
}
- (NSMutableArray *)titleArr{
    if (!_titleArr) {
        _titleArr = [NSMutableArray array];
    }
    return _titleArr;
}

@end
